package com.aibaixun.web.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import com.aibaixun.common.constant.Constants;
import com.aibaixun.common.core.domain.R;
import com.aibaixun.common.core.domain.model.LoginBody;
import com.aibaixun.system.domain.dto.DicmWorkOrderDto;
import com.aibaixun.system.domain.dto.WorkOrderAlarmDto;
import com.aibaixun.system.domain.dto.WorkOrderAlarmPageDto;
import com.aibaixun.system.domain.vo.WorkOrderAlarmAllVo;
import com.aibaixun.system.domain.vo.WorkOrderAlarmStatisticsAddressCountVo;
import com.aibaixun.system.domain.vo.WorkOrderAlarmStatisticsCountVo;
import com.aibaixun.system.domain.vo.WorkOrderAlarmStatisticsTrentCountVo;
import com.aibaixun.system.service.DicmWorkOrderService;
import com.aibaixun.system.service.SysLoginService;
import com.aibaixun.system.service.WorkOrderAlarmService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 第三方开放API信息管理
 *
 * @author zhangyh
 * @date 2023-09-01
 **/
@RestController
@RequestMapping("/openApi" )
public class OpenApiController {

    @Autowired
    private DicmWorkOrderService dicmWorkOrderService;

    @Autowired
    private WorkOrderAlarmService workOrderAlarmService;

    @Autowired
    private SysLoginService loginService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @SaIgnore
    @PostMapping("/login")
    public R<Map<String, Object>> login(@Validated @RequestBody LoginBody loginBody) {
        Map<String, Object> ajax = new HashMap<>();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
            loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return R.ok(ajax);
    }

    /**
     * 工单信息推送
     * @return
     */
    @ApiOperation(value = "工单信息推送")
    @PostMapping("/workOrder/push")
    public R workOrderPush(@RequestBody @Validated DicmWorkOrderDto dto){
        return dicmWorkOrderService.workOrderPush(dto);
    }

    /**
     * 分页查询工单告警
     * @return
     */
    @ApiOperation(value = "分页查询工单告警")
    @PostMapping("/alarm/page")
    public R<Page<WorkOrderAlarmAllVo>> getPage(@RequestBody @Validated WorkOrderAlarmPageDto pageDto){
        Page page = new Page();
        page.setCurrent(pageDto.getCurrent());
        page.setSize(pageDto.getSize());
        page.setSearchCount(true);
        return workOrderAlarmService.getPage(page, pageDto);
    }

    /**
     * 删除告警
     * @return
     */
    @ApiOperation(value = "删除告警")
    @GetMapping("/delete/{alarmId}")
    public R deleteAlarm(@PathVariable(value = "alarmId") Long alarmId){
        return workOrderAlarmService.deleteAlarm(alarmId);
    }

    /**
     * 获取告警分类统计
     * @return
     */
    @ApiOperation(value = "获取告警分类统计")
    @PostMapping("/alarm/statistics/type")
    public R<WorkOrderAlarmStatisticsCountVo> getCountStatisticsType(@RequestBody @Validated WorkOrderAlarmDto dto){
        return workOrderAlarmService.getCountStatisticsType(dto);
    }

    /**
     * 获取微魔方告警统计top5
     * @return
     */
    @ApiOperation(value = "获取微魔方告警统计top5")
    @PostMapping("/alarm/statistics/address")
    public R<List<WorkOrderAlarmStatisticsAddressCountVo>> getCountStatisticsAddress(@RequestBody @Validated WorkOrderAlarmDto dto){
        return workOrderAlarmService.getCountStatisticsAddress(dto);
    }

    /**
     * 获取告警趋势图
     * @return
     */
    @ApiOperation(value = "获取告警趋势图")
    @PostMapping("/alarm/statistics/trend")
    public R<List<WorkOrderAlarmStatisticsTrentCountVo>> getCountStatisticsTrend(@RequestBody @Validated WorkOrderAlarmDto dto){
        return workOrderAlarmService.getCountStatisticsTrend(dto);
    }



}
